package com.ych.modules.common.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ych.SDK.alibaba.ossSDK.AliOssUtil;
import com.ych.modules.base.dbEntity.TUserCardEntity;
import com.ych.modules.base.service.ITUserCardService;
import com.ych.modules.common.entity.query.QueryUserCardEntity;
import com.ych.modules.common.service.UserCardService;
import com.ych.utils.TLMap;
import com.ych.utils.UUID;
import com.ych.utils.beanToMap.BeanMapUtil;
import com.ych.utils.mysqlPageUtil.Query;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/3/20
 * Time: 16:21
 * Description:
 */
@Service("UserCardService")
public class UserCardServiceImpl implements UserCardService {
    @Autowired
    private ITUserCardService itUserCardService;
    @Autowired
    private AliOssUtil        aliOssUtil;

    @Override
    public void createUserCard(TUserCardEntity entity) {
        this.validateParam(entity);
        entity.setUserCardId(UUID.randomUUID());
        entity.setUserId(TLMap.getUserId());
        entity.setCreateTime(new Date());
        if (StringUtils.isBlank(entity.getCertName())) {
            entity.setCertName("暂无");
        }
        if (StringUtils.isBlank(entity.getCertId())) {
            entity.setCertId("-1");
        }
        if (StringUtils.isNotBlank(entity.getCardImg())) {
            entity.setCardImg(aliOssUtil.getRelativePath(entity.getCardImg()));
        }
        itUserCardService.insert(entity);
    }

    @Override
    public Page<TUserCardEntity> listUserCard(QueryUserCardEntity entity) {
        Wrapper<TUserCardEntity> wrapper = new EntityWrapper<TUserCardEntity>()
                .where("1=1")
                .eq(StringUtils.isNotBlank(entity.getUserId()), "user_id", entity.getUserId())
                .eq(StringUtils.isNotBlank(entity.getCardType()), "card_type", entity.getCardType())
                .orderBy("create_time desc");
        Page<TUserCardEntity> page = itUserCardService.selectPage(new Query<TUserCardEntity>(BeanMapUtil.toPageQuery(entity)).getPage(), wrapper);
        for (TUserCardEntity cardEntity : page.getRecords()) {
            if (StringUtils.isNotBlank(cardEntity.getCardImg())) {
                cardEntity.setCardImg(aliOssUtil.getOssObjectDownAuthUrl(cardEntity.getCardImg()));
            }
        }
        return page;
    }

    @Override
    public TUserCardEntity getUserCard(String cardId) {
        TUserCardEntity cardEntity = itUserCardService.selectById(cardId);
        if (null != cardEntity && StringUtils.isNotBlank(cardEntity.getCardImg())) {
            cardEntity.setCardImg(aliOssUtil.getOssObjectDownAuthUrl(cardEntity.getCardImg()));
        }
        return cardEntity;
    }

    /**
     * 参数校验
     *
     * @param entity
     */
    private void validateParam(TUserCardEntity entity) {
        if (StringUtils.isBlank(entity.getCardType())) {
            throw new RuntimeException("必传参数为空");
        }
        switch (entity.getCardType()) {
            case "10014001"://支付宝
                if (StringUtils.isBlank(entity.getAlipayAccount())) {
                    throw new RuntimeException("必传参数为空");
                }
                break;
            case "10014002"://微信
                if (StringUtils.isBlank(entity.getWxAccount())) {
                    throw new RuntimeException("必传参数为空");
                }
                break;
            case "10014003"://银行卡
                if (StringUtils.isBlank(entity.getBankName()) || StringUtils.isBlank(entity.getBankcardNum())) {
                    throw new RuntimeException("必传参数为空");
                }
                break;
            default:
                throw new RuntimeException("未知卡类型");
        }
    }
}
